﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace chap19 {
    class Program {
        public delegate int BinaryOp(int x, int y);

        private static void Main(string[] args) {
            Console.WriteLine("Chap 18");

            // Sycronized exampel
//            // Print out the ID of the executing thread.
//            Console.WriteLine("Main() invoked on thread {0}.",
//                              Thread.CurrentThread.ManagedThreadId);
//            // Invoke Add() in a synchronous manner.
//            BinaryOp b = Add;
//            // Could also write b.Invoke(10, 10);
//            int answer = b(10, 10);
//            // These lines will not execute until
//            // the Add() method has completed.
//            Console.WriteLine("Doing more work in Main()!");
//            Console.WriteLine("10 + 10 is {0}.", answer);

            // asyncronized examel
            // Print out the ID of the executing thread.
            Console.WriteLine("Main() invoked on thread {0}.",
                              Thread.CurrentThread.ManagedThreadId);
            // Invoke Add() on a secondary thread.
            BinaryOp b = Add;
            IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null);
            // Do other work on primary thread...
            Console.WriteLine("Doing more work in Main()!");
            // Obtain the result of the Add()
            // method when ready.
            int answer = b.EndInvoke(iftAR);
            Console.WriteLine("10 + 10 is {0}.", answer);

            Console.ReadLine();
        }


        private static int Add(int x, int y) {
            // Print out the ID of the executing thread.
            Console.WriteLine("Add() invoked on thread {0}.",
                              Thread.CurrentThread.ManagedThreadId);
            // Pause to simulate a lengthy operation.
            Thread.Sleep(5000);
            return x + y;
        }
    }
}